// import { SSL_OP_ALL } from 'constants';
import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';


interface Message {
	_id: string;
	client_type: string;
	productid: string;
	menu_name?: string;
	icon: string;
	description: string;
	priority: string;
	systemid: string;
	formid: string;
	refsystemid: string;
	is_show: string;
	background: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {

	const systemid = '12';
	const { update, exec } = operate(action_id, session_id, systemid);
	if (msg.menu_name) {
		const c = count(action_id, session_id, systemid);
		c.prepare('pub_menu', { productid: msg.productid, menu_name: msg.menu_name, client_type: msg.client_type });
		const [t] = await c.exec();
		// return t;
		if (t > 0) {
			return 'other';
		}
		update('pub_menu', { _id: msg._id, productid: msg.productid }, { menu_name: msg.menu_name, icon: msg.icon, description: msg.description, priority: msg.priority, systemid: msg.systemid, formid: msg.formid, refsystemid: msg.refsystemid, show_type: '', is_show: msg.is_show, background: msg.background });
	} else {
		update('pub_menu', { _id: msg._id, productid: msg.productid }, { icon: msg.icon, description: msg.description, priority: msg.priority, systemid: msg.systemid, formid: msg.formid, refsystemid: msg.refsystemid, show_type: '', is_show: msg.is_show, background: msg.background });
	}
	const res = await exec();
	if (res.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
